#ifndef _TELTrack
#define _TELTrack

#include<iostream.h>
#include<vector.h>

#include<TROOT.h>
#include<TFile.h>
#include<TTree.h>
#include<TMath.h>
#include<TF1.h>
#include<TGraph.h>
#include<TGraphErrors.h>

#include<Global.h>

class TELTrack{

  Global* glob;
  
  TFile* ff;
  TTree* ntuple;

 public:
  
  Int_t NumberOfEvents;
  
  vector<Float_t> SlopeDigX;
  vector<Float_t> ConstDigX;
  vector<Float_t> Chi2DigX;
  vector<Float_t> SlopeDigY;
  vector<Float_t> ConstDigY;
  vector<Float_t> Chi2DigY;

  vector<Float_t> SlopeCogX;
  vector<Float_t> ConstCogX;
  vector<Float_t> Chi2CogX;
  vector<Float_t> SlopeCogY;
  vector<Float_t> ConstCogY;
  vector<Float_t> Chi2CogY;

  vector<Float_t> HitDigX;
  vector<Float_t> HitDigY;
  vector<Float_t> Chi2Dig;

  vector<Float_t> HitCogX;
  vector<Float_t> HitCogY;
  vector<Float_t> Chi2Cog;

  Int_t RunNumber;
  Char_t* Path;

  // Fitting algorithm arrays
  
  Double_t * _planeX  ;
  Double_t * _planeEx ;
  Double_t * _planeY  ;
  Double_t * _planeEy ;
  
  Double_t * _planeDist ;
  Double_t * _planeScat ;
  
  Double_t * _fitX  ;
  Double_t * _fitEx ;
  Double_t * _fitY  ;
  Double_t * _fitEy ;
  
  Int_t arrayDim; 
  Double_t * _fitArray ;
  Double_t * _nominalFitArray ;
  Double_t * _nominalError; 

  // Parameters of fitting algorithm
  
  double _missingHitPenalty;
  double _skipHitPenalty;
  double _chi2Max ;
  bool   _useNominalResolution ;
  bool   _useDUT ;  
  bool   _useBeamConstraint ;
  double _beamSpread;
  Double_t _eBeam ;

  // Setup description
  
  int _nTelPlanes;
  int _nActivePlanes;
  int _iDUT;
  
  int * _planeSort;
  int * _planeID;
  double * _planeShiftX;
  double * _planeShiftY;
  double * _planeRotZ;
  double * _planePosition;
  double * _planeThickness;
  double * _planeX0;
  double * _planeResolution;
  bool   * _isActive;

  TELTrack(Int_t);
  ~TELTrack();

  void FindTracks(Int_t); 
  void FindHits(Int_t); 
  void PlotTracks();

  void GetHits(Int_t); 

  //AFZFit
  //! Fit particle track in one plane (XZ or YZ) 
  Int_t DoAnalFit(Double_t * pos, Double_t *err);

  //! Calculate Chi^2 of the fit
  /*! Calculate Chi^2 of the fit taking into account measured particle
   *  positions in X and Y and fitted scattering angles in XZ and YZ
   *  planes 
   */
  Double_t GetFitChi2();
  
  //! Solve matrix equation
  Int_t GaussjSolve(Double_t * alfa, Double_t * beta, Int_t n);

  void AFZMatrix(); 
  Double_t AFZFit(Float_t[], Float_t[]);
  Double_t NominalFit();

};

#endif
